/*
 DNN Content Builder - sourceforge.net/projects/dnncb
 Copyright (c) 2004-2007
 by Emerson Lopes (emersonlopes@gmail.com)

 Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated 
 documentation files (the "Software"), to deal in the Software without restriction, including without limitation 
 the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and 
 to permit persons to whom the Software is furnished to do so, subject to the following conditions:

 The above copyright notice and this permission notice shall be included in all copies or substantial portions 
 of the Software.

 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED 
 TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 
 THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF 
 CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 
 DEALINGS IN THE SOFTWARE.
*/

using System;
using System.Collections;
using System.Reflection;
using System.Xml.Serialization;

namespace DNNContentBuilder.UI.WebControls.DataBinding
{
	/// <summary>
	/// Custom data binding model class suitable for use with 
	/// <see cref="System.Colections.IEnumerable"/> data sources.
	/// </summary>
	public class EnumerableBindingModel : AbstractBindingModel
	{
		private IEnumerable _enumerable;
		private string[] _columns;
		private string _name;
		private int _count;

		public EnumerableBindingModel(IEnumerable enumerable)
		{
			_count		= -1;
			_enumerable = enumerable;
		}

		public override int Count
		{
			get
			{
				if (_count == -1)
				{
					_count = 0;
					IEnumerator enumerator = _enumerable.GetEnumerator();

					while (enumerator.MoveNext()) _count++;
				}

				return _count;
			}
		}

		public override string Name
		{
			get
			{
				if (_name == null)
				{
					_name = Guid.NewGuid().ToString();
				}

				return _name;
			}
		}

		/// <summary>
		/// Convert all public properties to column names
		/// </summary>
		/// <returns></returns>
		public override string[] GetColumns()
		{
			if (_columns == null)
			{
				IEnumerator enumerator = _enumerable.GetEnumerator();
				
				if (enumerator.MoveNext())
				{
					// Get the type of the first item in the collection (assume it's homogeneous)
					Type t = enumerator.Current.GetType();
					PropertyInfo[] properties = t.GetProperties(BindingFlags.Instance|BindingFlags.Public);

					_columns = new string[properties.Length];

					for (int i = 0; i < properties.Length; i++)
					{
						_columns[i] = properties[i].Name;
					}
				}
			}

			return _columns;
		}

		public override void WriteXml(System.IO.TextWriter writer)
		{
			if (_enumerable == null || this.Count == 0)
			{
				return;
			}

			XmlSerializer serializer = new XmlSerializer(_enumerable.GetType());
			serializer.Serialize(writer, _enumerable);
		}
	}
}
